﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="false"
    CodeFile="sMenuRightslist.aspx.cs" Inherits="Training.Web.Lists.Administration.UserManagement.sMenuRightslist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="body" runat="Server">
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.msgBox.v1.js") %>" type="text/javascript"></script>
    <div class="page-full-width cf">
        <div class="content-module">
           <%-- <div class="content-module-heading cf">
                <h3 class="fl">
                    Hak Akses Menu</h3>
                <span class="fr expand-collapse-text">Click to collapse</span> <span class="fr expand-collapse-text initial-expand">
                    Click to expand</span>
            </div>--%>
            <!-- end content-module-heading -->
            <div class="content-module-main">
                <div>
                    <table cellpadding="0" cellspacing="0" border="0">
                        <tr>
                            <td>
                                Nama Role
                            </td>
                            <td>
                                &nbsp;:&nbsp;
                            </td>
                            <td>
                                <asp:DropDownList ID="ddlRole" runat="server">
                                </asp:DropDownList>
                            </td>
                        </tr>
                    </table>
                </div>
                <table id="table" class="scroll" cellpadding="0" cellspacing="0">
                </table>
                <div id="pager" class="scroll">
                </div>
                <br />
                <div id="dialog" title="Default dialog">
                </div>
            </div>
            <div>
                <button id="btnSave" type="button">
                    Save</button>
            </div>
        </div>
        <!-- end content-module-main -->
    </div>
    </div>
    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });

    </script>
    <script type="text/javascript">

        var grid = $('#table');
        grid.jqGrid({
            url: '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sMenuhandler.ashx") %>?method=getdata',
            datatype: 'json',
            colNames: [
             'ID',
             'Nama Menu',
             'Lihat <input type="checkbox" id="cbHeaderView" onclick="cbAll(\'View\', event)" />',
             'Tambah <input type="checkbox" id="cbHeaderAdd" onclick="cbAll(\'Add\', event)" />',
             'Ubah <input type="checkbox" id="cbHeaderEdit" onclick="cbAll(\'Edit\', event)" />',
             'Hapus <input type="checkbox" id="cbHeaderDelete" onclick="cbAll(\'Delete\', event)" />',
             'Review <input type="checkbox" id="cbHeaderReview" onclick="cbAll(\'Review\', event)" />',
             'Approve <input type="checkbox" id="cbHeaderApprove" onclick="cbAll(\'Approve\', event)" />',
             'Parent ID'],
            colModel: [
           		{ name: 'MenuID', hidden: true, index: 'MENUID', width: 60 },
           		{ name: 'MenuName', index: 'MENUNAME', width: 600 },
           		{ name: 'VIEW', index: 'VIEW', width: 310, align: 'center', sortable: false },
           		{ name: 'ADD', index: 'ADD', width: 310, align: 'center', sortable: false },
           		{ name: 'EDIT', index: 'EDIT', width: 310, align: 'center', sortable: false },
           		{ name: 'DELETE', index: 'DELETE', width: 310, align: 'center', sortable: false },
                { name: 'REVIEW', hidden: true, index: 'REVIEW', width: 310, align: 'center', sortable: false },
                { name: 'APPROVE', hidden: true, index: 'APPROVE', width: 310, align: 'center', sortable: false },

           		{ name: 'ParentMenuID', hidden: true, index: 'PARENTMENUID', width: 60 }
           	],
            rownumbers: true,
            rowNum: 100,
            id: 'MenuID',
            multiselect: false,
            sortname: 'ORDERSEQ',
            sortorder: 'asc',
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 630,
            height: '100%',
            caption: 'Hak Akses Menu',
            ajaxGridOptions: { cache: false },
            gridComplete: function () {
                var ids = grid.jqGrid('getDataIDs');
                for (var i = 0; i < ids.length; i++) {
                    var value = grid.jqGrid('getCell', ids[i], 'MenuID');
                    v = '<input type="checkbox" onchange="DoChange(\'View\', \'' + value + '\');" value="' + value + '" name="checkboxView_' + value + '" id="checkboxView_' + value + '" />';
                    a = '<input type="checkbox" onchange="DoChange(\'Add\', \'' + value + '\');" value="' + value + '" name="checkboxAdd_' + value + '" id="checkboxAdd_' + value + '" />';
                    e = '<input type="checkbox" onchange="DoChange(\'Edit\', \'' + value + '\');" value="' + value + '" name="checkboxEdit_' + value + '" id="checkboxEdit_' + value + '" />';
                    d = '<input type="checkbox" onchange="DoChange(\'Delete\', \'' + value + '\');" value="' + value + '" name="checkboxDelete_' + value + '" id="checkboxDelete_' + value + '" />';
                    r = '<input type="checkbox" onchange="DoChange(\'Review\', \'' + value + '\');" value="' + value + '" name="checkboxReview_' + value + '" id="checkboxReview_' + value + '" />';
                    o = '<input type="checkbox" onchange="DoChange(\'Approve\', \'' + value + '\');" value="' + value + '" name="checkboxApprove_' + value + '" id="checkboxApprove_' + value + '" />';

                    grid.jqGrid('setRowData', ids[i], { VIEW: v, ADD: a, EDIT: e, DELETE: d, REVIEW: r, APPROVE: o });

                }
            }
        });

        function SetCheckBox(src, val) {
            var ids = grid.jqGrid('getDataIDs');
            for (var i = 0; i <= ids.length; i++) {
                var menuID = grid.jqGrid('getCell', ids[i], 'MenuID');
                $('#table' + ' input[name=checkbox' + src + '_' + menuID + ']').prop('checked', val);
            }

            $('#cbHeader' + src).prop('checked', val);
        }

        function cbAll(src, e) {
            e = e || event; /* get IE event ( not passed ) */
            e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
            var chkSelectAll = $('#cbHeader' + src);
            if (chkSelectAll.length && chkSelectAll.is(':checked') && !status) {
                SetCheckBox(src, true);
            }
            else {
                SetCheckBox(src, false);
            }
        }

        function LoadMenuChecked() {
            SetCheckBox('View', false);
            SetCheckBox('Add', false);
            SetCheckBox('Edit', false);
            SetCheckBox('Delete', false);
            SetCheckBox('Review', false);
            SetCheckBox('Approve', false);

            var roleId = $('#<%= ddlRole.ClientID %> option:selected').val();
            if (roleId > -1) {
                $.ajax({
                    type: 'POST',
                    url: '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sMenuRightshandler.ashx") %>?method=getdatabyroleid&roleId=' + roleId,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (datas) {
                        $.each(datas, function (i, data) {
                            var ids = grid.jqGrid('getDataIDs');
                            for (var i = 0; i <= ids.length - 1; i++) {
                                var menuID = grid.jqGrid('getCell', ids[i], 'MenuID');
                                if (data.AccessCode != '') {
                                    if (menuID == data.MenuID) {
                                        var arrAccessCode = data.AccessCode.split('|');
                                        for (var a = 0; a <= arrAccessCode.length; a++) {
                                            if (arrAccessCode[a] == 'V') {

                                                $('#table' + ' input[name=checkboxView_' + menuID + ']').prop('checked', true);

                                            }

                                            if (arrAccessCode[a] == 'A') {

                                                $('#table' + ' input[name=checkboxAdd_' + menuID + ']').prop('checked', true);

                                            }

                                            if (arrAccessCode[a] == 'E') {

                                                $('#table' + ' input[name=checkboxEdit_' + menuID + ']').prop('checked', true);

                                            }

                                            if (arrAccessCode[a] == 'D') {

                                                $('#table' + ' input[name=checkboxDelete_' + menuID + ']').prop('checked', true);

                                            }

                                            if (arrAccessCode[a] == 'R') {

                                                $('#table' + ' input[name=checkboxReview_' + menuID + ']').prop('checked', true);

                                            }

                                            if (arrAccessCode[a] == 'O') {

                                                $('#table' + ' input[name=checkboxApprove_' + menuID + ']').prop('checked', true);

                                            }



                                        }
                                    }

                                }

                            }
                        });
                    },
                    error: function () {
                        alert('Failed to load menu checked');
                    }
                });
            }

        }

        function DoChange(src, val) {
            $('#cbHeader' + src).prop('checked', false);
            var thisVal = $('#table' + ' input[name=checkbox' + src + '_' + val + ']').is(':checked');
            if (thisVal == false)
                return;
            var ids = grid.jqGrid('getDataIDs');
            var selMenu = '';
            $.each(ids, function () {
                selMenu = grid.jqGrid('getCell', this, 'MenuID');
                if (val == selMenu) {
                    var parentID = grid.jqGrid('getCell', this, 'ParentMenuID')

                    if (!($('#table' + ' input[name=checkbox' + src + '_' + parentID + ']').is(':checked'))) {
                        $('#table' + ' input[name=checkbox' + src + '_' + parentID + ']').prop('checked', true);
                    }
                    return;
                }
            });
        }

        $('#<%= ddlRole.ClientID %>').change(function () {
            LoadMenuChecked();
        });

        $(document).ready(function () {

            $('#btnSave').click(function () {

                var roleId = $('#<%= ddlRole.ClientID %> option:selected').val();

                if (roleId !== '') {

                    var ids = grid.jqGrid('getDataIDs');

                    var ACCESS_CODE = '';

                    var selData = [];

                    for (var i = 0; i <= ids.length - 1; i++) {

                        var menuID = grid.jqGrid('getCell', ids[i], 'MenuID');

                        var chView = $('#table' + ' input[name=checkboxView_' + menuID + ']').prop('checked');
                        var chAdd = $('#table' + ' input[name=checkboxAdd_' + menuID + ']').prop('checked');
                        var chEdit = $('#table' + ' input[name=checkboxEdit_' + menuID + ']').prop('checked');
                        var chDelete = $('#table' + ' input[name=checkboxDelete_' + menuID + ']').prop('checked');
                        var chReview = $('#table' + ' input[name=checkboxReview_' + menuID + ']').prop('checked');
                        var chApprove = $('#table' + ' input[name=checkboxApprove_' + menuID + ']').prop('checked');


                        ACCESS_CODE = '';

                        if (chView) {
                            ACCESS_CODE += 'V|';
                        }

                        if (chAdd) {
                            ACCESS_CODE += 'A|';
                        }

                        if (chEdit) {
                            ACCESS_CODE += 'E|';
                        }

                        if (chDelete) {
                            ACCESS_CODE += 'D|';
                        }

                        if (chReview) {
                            ACCESS_CODE += 'R|';
                        }

                        if (chApprove) {
                            ACCESS_CODE += 'O|';
                        }



                        if (ACCESS_CODE != '') {
                            sysTMenuRights = new Object()
                            sysTMenuRights.RoleID = roleId;
                            sysTMenuRights.MenuID = menuID;
                            sysTMenuRights.AccessCode = ACCESS_CODE;
                            selData.push(sysTMenuRights);
                        }
                    }

                    var JSONData = JSON.stringify(selData);
                    var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sMenuRightshandler.ashx") %>';

                    CallAjax(handlerUrl, "?method=insert", JSONData);

                    setTimeout(" window.location.reload();", 3000);

                }
                else {

                    //alert('Silahkan pilih Role terlebih dahulu !');
                    $.msgBox("Silahkan pilih Role terlebih dahulu !", null, { title: "Pesan" });

                }
            });
        });
       
    </script>
</asp:Content>
